package com.ophelisis.common.util.xstream;

import java.io.Writer;

import com.thoughtworks.xstream.core.util.QuickWriter;
import com.thoughtworks.xstream.io.HierarchicalStreamWriter;
import com.thoughtworks.xstream.io.xml.PrettyPrintWriter;
import com.thoughtworks.xstream.io.xml.StaxDriver;

public class BaseStaxDriver extends StaxDriver {
	
	public HierarchicalStreamWriter createWriter(Writer out) {
		return new CdataWriter(out);
	}

	public class CdataWriter extends PrettyPrintWriter {
		public static final String CDATA_PREFIX = "<![CDATA[";
		public static final String CDATA_SUFFIX = "]]>";

		public CdataWriter(Writer writer) {
			super(writer);
		}
		
		protected void writeText(QuickWriter writer, String text) {
			boolean isCdata = text.startsWith(CDATA_PREFIX) && text.endsWith(CDATA_SUFFIX);
			if(isCdata) {
				writer.write(text);
			} else {
				super.writeText(writer, text);
			}
		}
	}
}
